/*
 * @Author: wei.yue 
 * @Date: 2018-10-26 11:52:29 
 */

import { ServerConfig } from "../config/ServerConfig";

enum RequestType {
    POST = 1,
    GET = 2,
    PUT = 3,
    DELETE = 4,
}

export default class Http {
    baseURL: string = ServerConfig.URL;
    authorization: any = null;

    constructor(url: string = null) {
        if (url) {
            this.baseURL = url;
        }
    }

    setBaseURL(url) {
        this.baseURL = url;
    }

    setAuthorization(authorization: any) {
        this.authorization = authorization;
    }

    send(requestType: RequestType, url: string, callback: Function, param: any = {}, err: boolean = false) {
        let xhr = cc.loader.getXMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    let respone = xhr.responseText;
                        let data = JSON.parse(respone);
                        console.log("=================收到http返回数据begin============");
                        console.log("status:" + xhr.status);
                        console.log("data:" ,data);
                        console.log("=================收到http返回数据end==============");
                        if (!err && data.code == ServerConfig.HTTP_CODE.SUCCEED) {
                            if (callback) {
                                callback(data);
                            }
                        } else {
                            if (callback) {
                                callback(data);
                            }
                        }
                } else {
                    console.log("status:" + xhr.status);
                }
            }
        };
 
        console.log("====================发送http请求begin==================");
        console.log("url:" + url);
        console.log(param);
        console.log("====================发送http请求end==================");
        switch (requestType) {
            case RequestType.GET:
                xhr.open("GET", url, true);
                xhr.timeout = ServerConfig.TIMEOUT;
                break;
            case RequestType.POST:
                xhr.open("POST", url, true);
                xhr.timeout = ServerConfig.TIMEOUT;
                xhr.setRequestHeader("Content-Type", "application/json");
                break;
            case RequestType.PUT:
                xhr.open("PUT", url, true);
                xhr.timeout = ServerConfig.TIMEOUT;
                xhr.setRequestHeader("Content-Type", "application/json");
                break;
            case RequestType.DELETE:
                xhr.open("DELETE", url, true);
                xhr.timeout = ServerConfig.TIMEOUT;
                xhr.setRequestHeader("Content-Type", "application/json");
                break;
        }
        if (this.authorization != null) {
            xhr.setRequestHeader("authorization", this.authorization);
        }
        if (requestType == RequestType.GET) {
            xhr.send();
        } else {
            xhr.send(JSON.stringify(param));
        }
    }

    doGet(api: string, callback: Function, param: any = null, err: boolean = false) {
        this.send(RequestType.GET, this.baseURL + api, callback, param, err);
    }

    doPost(api: string, callback: Function, param: any, err: boolean = false) {
        this.send(RequestType.POST, this.baseURL + api, callback, param, err);
    }

    doPut(api: string, callback: Function, param: any, err: boolean = false) {
        this.send(RequestType.PUT, this.baseURL + api, callback, param, err);
    }

    doDelete(api: string, callback: Function, param: any, err: boolean = false) {
        this.send(RequestType.DELETE, this.baseURL + api, callback, param, err);
    }
}

